import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
			uploadNum: 0,
      total: 0, //总条数
      page: 1, //当前页
      size: 10, //每页显示条数
			modelShow: false,
			ismodelShow: false,
      friendlyLinkList: [], //友情连接列表
      portalConfigList: [], //门户配置列表
      validate: {}, //验证对象(友情连接)
      validate2: {}, //验证对象(其他连接)
      portalConfig: {} //门户配置信息
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
    //初始化友情连接列表分页信息
    this.searchFriendlyLinkList();
    //初始化门户配置列表分页信息
    this.searchPortalConfigList();
	},
	mounted() {
    //加载表单验证
    this.validate = $("#portalConfigForm").validate({
      ignore: [],
      rules: {
        name: { // 连接名称
          required: true,
          maxlength: 100
        },
        url: { // 连接地址
          required: true,
          maxlength: 200
        }
      },
      messages: {
        name: { // 连接名称
          required: "请输入连接名称",
          maxlength: "请输入长度小于100"
        },
        url: { // 连接地址
          required: "请输入连接地址",
          maxlength: "请输入长度小于200"
        }
      },
      errorElement: "em",
      errorPlacement: function (error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }
    });

    this.validate2 = $("#portalConfigForm2").validate({
      ignore: [],
      rules: {
        name: { // 连接名称
          required: true,
          maxlength: 100
        },
        url: { // 连接地址
          required: true,
          maxlength: 200
        }
      },
      messages: {
        name: { // 连接名称
          required: "请输入连接名称",
          maxlength: "请输入长度小于100"
        },
        url: { // 连接地址
          required: "请输入连接地址",
          maxlength: "请输入长度小于200"
        }
      },
      errorElement: "em",
      errorPlacement: function (error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }
    });
  },
	methods: { //写函数方法
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
			this.searchFriendlyLinkList()
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
		openModal: function(id) {
      let that = this;
		  this.modelShow = true;
      that.portalConfig = {};
			//这个地方可以调用接口，也可以获取当前列表信息，具体待定
			if (id != null) { //如果有id，则查询该
        that.initPortalConfig(id);
      }
		},
		isopenModal: function(id) {
      let that = this;
		  this.ismodelShow = true;
      that.portalConfig = {};
      //这个地方可以调用接口，也可以获取当前列表信息，具体待定
      if (id != null) { //如果有id，则查询该
        that.initPortalConfig(id);
      }
		},
		closeModel: function() {
			this.modelShow = false;
			this.ismodelShow = false;
		},
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
		idFrontImg: function(event) {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
      .then(function(res) {
        if(res.success) {
          let fileInfo = res.data[0];
          $(".showHandIdFront").attr("src",that.urls.baseUrl + fileInfo.url);
          $(".handIdFrontA").css("display", "none");
          $("input[name='url']").val(fileInfo.url);
        }
      })


		},
    //初始化对象
    initPortalConfig: function(id) {
      let that = this;
		  that.$post(
        that.urls.getPortalConfig,
        {"id": id}
      )
      .then(function(res) {
        if(res.success) {
          that.portalConfig = res.data;
        }
      })
    },
		//添加新连接
		addlinks: function() {
			let that = this;
      let flg = that.validate.form();
      if (flg) {
        let dataObj = that.combinationFormObject(that.validate.currentForm);
        that.$post(
          that.urls.addPortalConfig,
          dataObj
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            console.log(res);
            that.modelShow = false;
            msg = "添加友情连接成功！";
            that.searchFriendlyLinkList();
          } else {
            msg = "添加友情连接失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
		},
    //修改连接
    updateData: function(type) {
      let that = this;
		  let flg = false;
		  if (type == 0) {
        flg = that.validate2.form();
      } else if (type == 1) {
        flg = that.validate.form();
      }
		  if (flg) {
        let dataObj = {};
        if (type == 0) {
          dataObj = that.combinationFormObject(that.validate2.currentForm);
        } else {
          dataObj = that.combinationFormObject(that.validate.currentForm);
        }
        that.$post(
          that.urls.updatePortalConfig,
          dataObj
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            if (type == 1) {
              msg = '修改友情连接成功！';
              //重新渲染数据
              that.searchFriendlyLinkList();
            } else {
              msg = '修改其他连接成功！';
              //重新渲染数据
              that.searchPortalConfigList();
            }
          } else {
            let m = type == 0 ? "其他" : "友情";
            msg = '修改" + m + "连接失败！';
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //删除连接
    deleteData: function(id,type) {
      if (id != null && type != null) {
        let that = this;
        that.$post(
          that.urls.deletePortalConfig,
          {"id": id}
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = '删除门户" + (type == 1 ? "友情连接" : "其他配置") + "成功！';
            //重新渲染数据
            that.searchTodo();
          } else {
            msg = '删除" + (type == 1 ? "友情连接" : "其他配置") + "失败！';
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
        if (type == 0) { //渲染门户基础信息
          that.searchPortalConfigList();
        } else if (type == 1) { //渲染门户友情信息
          that.searchFriendlyLinkList();
        }
      }
    },
		//友情链接列表
    searchFriendlyLinkList: function() {
			let that = this;
			that.$post(
					that.urls.getPortalConfigPageInfo,
          {"pageNum": that.page,"pageSize": that.size,"type": 1}
				)
				.then(function(res) {
					if(res.success) {
						console.log("友情链接列表：" + res);
            that.friendlyLinkList = res.pageInfo.list;
            that.total = res.pageInfo.total;
            that.page = res.pageInfo.pageNum;
					} else {
						layer.alert(res.message);
					}
				})
		},
    //门户配置列表
    searchPortalConfigList: function() {
      let that = this;
      that.$post(
        that.urls.getPortalConfigPageInfo,
        {"pageNum": 0,"pageSize": 2,"type": 0}
      )
        .then(function(res) {
          if(res.success) {
            that.portalConfigList = res.pageInfo.list;
          } else {
            layer.alert(res.message);
          }
        })
    }
	}
}
